
import { DICT_TYPE, getIntDictOptions } from "@/utils/dict";
import { Col, Form, Input, Modal, Row, Select, InputNumber, Flex, Radio, Spin, TreeSelect, message } from "antd";
import React, { useState, useEffect, useRef, useImperativeHandle, } from "react";
import { classApi, brandApi } from "@/api";
import XxFormItemUpload from "@/ui/xx-AliyunOSSUpload/xx-FormItemUpload";
import { UploadPhotoToServer } from "@/ui";
const BrandForm = ({ onRef, getList = Function }) => {
    const [treeData, setTreeData] = useState([]);
    const [show, setShow] = useState(false);
    const [loading, setLoading] = useState(false);
    const [form] = Form.useForm();

    /**@description 加载行业树 */
    const loadClassTree = async () => {
        const { data } = await classApi.getClassList();
        setTreeData(data?.list ? data.list[0].children : [])
    }


    /**@description 表单布局 */
    const formItemLayout = {
        labelCol: {
            xs: {
                span: 24,
            },
            sm: {
                span: 4,
            },
        },
        wrapperCol: {
            xs: {
                span: 24,
            },
            sm: {
                span: 20,
            },
        },
    };

    /**@description 取消 */
    const onCanle = () => {
        setShow(false);
    }

    /**@description 添加|修改操作 */
    const open = async ({
        type = String || undefined,
        record = {},
    }) => {
        const { id = undefined, status = "1" } = record;
        setShow(true);
        loadClassTree();
        form.resetFields();
        form.setFieldValue("type", "action." + type)
        form.setFieldValue("status", status)



        if (type === "update") {

            if (id) {
                try {
                    setLoading(true);
                    const { data } = await brandApi.getBrand(id);
                    console.log("编辑，详情", data)
                    form.setFieldsValue({
                        type: type || undefined,
                        ...data
                    });


                } catch (error) {

                } finally {
                    setLoading(false);
                }
            }
        } else if (type === "add") {
            // // 新增 
            form.setFieldsValue({
                brandType: "1"
            })
        } else {
        }


    }

    /**@description 异步保存数据 */
    const createBrand = async (value) => {

        try {
            const data = await brandApi.createBrand(value);
            getList();
            onCanle();
        } catch (error) {

        } finally {

        }

    }

    /**@description 校验表单 */
    const validateForm = async () => {
        try {
            const values = await form.validateFields();
            let paramsFormData = {
                ...values,
            }
            createBrand(paramsFormData)
        } catch (error) {
            console.log("校验失败", error)
        }
    }

    /**@description 向外暴露方法 */
    useImperativeHandle(onRef, () => ({
        open
    }))



    useEffect(() => {
        show && loadClassTree();
        !show && form.resetFields()
    }, [show])

    const formProps = {
        form,
        rules: {
            brandName: [{ required: true, message: '必须输入品牌名称' }],
            classId: [{ required: true, message: '必须选择行业类目' }],
            brandIcon: [{ required: true, message: '请选择品牌logo' }],
            status: [{ required: true, message: '状态选项为必选项' }],
        }
    }

    return (
        <>
            <Modal
                title="品牌编辑"
                width={550}
                open={show}
                centered={true}
                destroyOnClose={true}
                onCancel={onCanle}
                onOk={validateForm}
            >
                <Spin spinning={loading}>
                    <Form
                        {...formItemLayout}
                        {...formProps}
                    >
                        <Row gutter={24}>
                            <Col span={24} style={{ display: "none" }} >
                                <Form.Item label="id" name="id">
                                    <Input />
                                </Form.Item>
                            </Col>
                            <Col span={24}>
                                <Form.Item
                                    rules={formProps.rules["brandName"]}
                                    label="品牌名称" name="brandName">
                                    <Input placeholder="请输入品牌名称" />
                                </Form.Item>
                            </Col>
                            <Col span={24}>
                                <Form.Item
                                    rules={formProps.rules["classId"]}
                                    label="行业类目" name="classId">
                                    <TreeSelect
                                        // disabled={disabled}
                                        treeData={treeData}
                                        multiple
                                        fieldNames={{
                                            label: "className",
                                            value: "id"
                                        }}
                                    />

                                </Form.Item>
                            </Col>
                            <Col span={24}>
                                <Form.Item label="品牌LOGO"
                                    name="brandIcon"
                                    rules={formProps.rules["brandIcon"]}>
                                    <UploadPhotoToServer
                                        maxCount={1}
                                        style={{ width: 100, height: 100 }}
                                    />
                                </Form.Item>
                            </Col>
                            <Col span={24}>
                                <Form.Item label="显示排序" name="sort">
                                    <InputNumber />
                                </Form.Item>
                            </Col>

                            <Col span={24}>
                                <Form.Item
                                    rules={formProps.rules["status"]}
                                    label="品牌状态" name="status">
                                    <Radio.Group
                                        options={getIntDictOptions(DICT_TYPE.PRODUCT_BRAND_STATUS)}
                                        optionType="button"
                                        buttonStyle="solid"
                                    />
                                </Form.Item>
                            </Col>
                        </Row>
                    </Form>
                </Spin>

            </Modal >
        </>
    )
}
export default BrandForm;